package com.aiyige.page.my.favorite;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.entity.FavoriteRecord;
import com.aiyige.model.request.MyFavoriteRequest;
import com.aiyige.model.response.MyFavoriteListResponse;
import com.aiyige.page.my.favorite.adapter.MyFavoriteAdapter;
import com.aiyige.utils.HighLightWordUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.CommonDataView;
import com.aiyige.utils.widget.MyEditText;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.vondear.rxtools.RxKeyboardTool;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by xiachao on 2017/11/29.
 */
@Route(path = ARouterConfig.MyFavoriteSearchTypePage)
public class SearchTypeFavoritePage extends BaseActivity implements CommonDataView.RetrofitStub {

    private static final String TAG = "SearchTypeMyOrderPage";

    @BindView(R.id.cdv)
    CommonDataView cdv;

    MyFavoriteAdapter adapter;

    @BindView(R.id.et_search)
    MyEditText inputEt;

    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    @BindView(R.id.cancelLayout)
    ExpandableLayout cancelLayout;


    @Autowired
    public String subject;

    @Autowired
    public String parentSearchValue;

    public static void start(String subject, String parentSearchValue) {
        ARouter.getInstance().build(ARouterConfig.MyFavoriteSearchTypePage)
                .withString("subject", subject)
                .withString("parentSearchValue", parentSearchValue).navigation();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_search_type_my_orders);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        initViews();
    }

    private void initViews() {
        inputEt.getEditText().setText(parentSearchValue);
        adapter = new MyFavoriteAdapter();
        cdv.config(adapter, this);
        cdv.doRefreshRequest();
        cdv.addItemDecoration(1, R.color.zwcolor_gray_e3e3e3);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(this, R.color.zwcolor_gray_f4f8f9));
        inputEt.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showSearchResultPanel();
                }
            }
        });
        inputEt.setActionSearch(new MyEditText.ActionCallback() {
            @Override
            public void actionSearch(String value) {
                doSearch();
            }
        });
    }


    @Override
    public Call<ResponseBody> call(long currentPage) {
        List<String> list = new ArrayList<>();
        if (!isEmpty(subject)) {
            if (subject.equals(Subject.dynamic)) {
                list.addAll(Subject.getDynamicSubjects());
            }
        } else {
            list.add(subject);
        }
        return MyFavoriteRequest.newBuilder()
                .pageNum(currentPage)
                .keyWord(getSearchValue())
                .subject(list)
                .build();
    }

    private String getSearchValue() {
        return isEmpty(inputEt.getEditText().getText().toString().trim()) ? "" : inputEt.getEditText().getText().toString().trim();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            MyFavoriteListResponse homeResponse = JSON.parseObject(respJson, MyFavoriteListResponse.class);
            if (ListUtil.isEmpty(homeResponse.getContent())) {
                resultList.addAll(homeResponse.getContent());
                return new CommonDataView.HandleResult(homeResponse.isLast());
            } else {
                for (FavoriteRecord record : homeResponse.getContent()) {
                    record.setMomentTitle(HighLightWordUtil.highLight(record.getMomentTitle(),getSearchValue()));
                }

                resultList.addAll(homeResponse.getContent());
                return new CommonDataView.HandleResult(homeResponse.isLast());
            }
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }

    }


    public void showSearchResultPanel() {
        cancelLayout.expand();
        cdv.setVisibility(View.VISIBLE);
    }

    public void hideSearchResultPanel() {
        finish();
    }

    private void doSearch() {
        cdv.refresh();
        RxKeyboardTool.hideSoftInput(this);
    }


    @OnClick({R.id.tv_cancel, R.id.cancelLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
            case R.id.cancelLayout:
                hideSearchResultPanel();
                break;
        }
    }
}
